using System;
using System.Collections.Specialized;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Globalization;

namespace AresControls
{
	public class HInput : HControl, IPostBackDataHandler
	{
		private string m_Value = "", m_DefaultStyle = "", m_Format = "", m_Comma = "", m_Decimal = ".", m_Sufix = "", m_Prefix = "", m_NumericFieldType = "";
		private int m_nDecimals = 0, m_MaxLength = 0;
		private bool m_Positive, m_StrictPositive, m_Negative, m_StrictNegative, m_Password;

		public HInput()
		{
			m_DefaultStyle = ";padding:0;height:18;font-size:8pt;font-family:Arial;margin:0;border-width:0;color:#442200;text-align:right;background-color:#FFFFFF;";
			Positive = StrictPositive = Negative = StrictNegative = Password = false;
		}

		public bool Password
		{
			get { return m_Password; }
			set { m_Password = value; }
		}

		public bool Positive
		{
			get { return m_Positive; }
			set { m_Positive = value; }
		}

		public bool StrictPositive
		{
			get { return m_StrictPositive; }
			set { m_StrictPositive = value; }
		}

		public bool Negative
		{
			get { return m_Negative; }
			set { m_Negative = value; }
		}

		public bool StrictNegative
		{
			get { return m_StrictNegative; }
			set { m_StrictNegative = value; }
		}

		public int MaxLength
		{
			get { return m_MaxLength; }
			set { m_MaxLength = value; }
		}		

		public string NumericFieldType
		{
			get { return m_NumericFieldType; }
			set { m_NumericFieldType = value; }
		}

		public string FormatString
		{
			get { return m_Format; }
			set { m_Format = value; }
		}

		public string Value
		{
			get { return m_Value; }
			set { m_Value = value; }
		}

		public int NrOfDecimals
		{
			get
			{
				DoGetFormatValues();
				return m_nDecimals;
			}
		}

		private string DecimalSep
		{
			get { return m_Decimal; }
			set { m_Decimal = value; }
		}

		private string CommaSep
		{
			get { return m_Comma; }
			set { m_Comma = value; }
		}

		private int DecimalsNo
		{
			get { return m_nDecimals; }
			set { m_nDecimals = value; }
		}

		private string Prefix
		{
			get { return m_Prefix; }
			set { m_Prefix = value; }
		}

		private string Sufix
		{
			get { return m_Sufix; }
			set { m_Sufix = value; }
		}

		public bool LoadPostData(String postDataKey, NameValueCollection val)
		{
			m_Value = (GridControl)?"":Unformat((string)val[UniqueID]);
			return false;
		}

		protected void DoGetFormatValues()
		{
			if (FormatString != "" && FormatString.IndexOf("#")>=0)
			{
				string strVal = FormatString;
				char ch;

				while (strVal.Length >= 2)
				{
					strVal = strVal.Substring(1);
					int iPos;
					ch = strVal[0];
					iPos = strVal.IndexOf("#");
					if (ch == 'd')
					{
						DecimalSep = (string)strVal.Substring(1,iPos-1);
						iPos = 2;
					}
					if (ch == 'c')
					{
						CommaSep = (string)strVal.Substring(1,iPos-1);
						iPos = 2;
					}
					if (ch == 'n')
						DecimalsNo = Int32.Parse(strVal.Substring(1,iPos-1));
					if (ch == 'p')
						Prefix = strVal.Substring(1,iPos-1);
					if (ch == 's')
						Sufix = strVal.Substring(1,iPos-1);
					strVal = strVal.Substring(iPos);
				}
			}
			else
				if (FormatString == "")
					DecimalSep = "";
		}

		protected string Unformat(string strVal)
		{
			DoGetFormatValues();
			if (strVal == "" || strVal == null)
				return strVal;

			strVal = (Sufix != "")?strVal.Substring(0,strVal.Length-Sufix.Length):strVal;
			strVal = (Prefix != "")?strVal.Substring(Prefix.Length):strVal;
			strVal = (CommaSep != "")?strVal.Replace(CommaSep, ""):strVal;
			strVal = (DecimalSep != "")?strVal.Replace(DecimalSep, "."):strVal;

			return strVal;
		}

		protected string Format(string strVal)
		{
			if ((strVal != "") && (strVal != null) && DecimalSep != "")
			{
				NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;
				nfi.NumberDecimalDigits = DecimalsNo;
				nfi.NumberDecimalSeparator = DecimalSep;
				nfi.NumberGroupSeparator = CommaSep;
				strVal = Double.Parse(strVal).ToString("n", nfi);
				strVal = Prefix + strVal + Sufix;
			}
			return strVal;
		}

		protected override void Render(HtmlTextWriter output)
		{
			StringBuilder sb = new StringBuilder();
			DoGetFormatValues();

			Style += (ReadOnly)?";background-color:"+ReadOnlyColor:"";

			if (GridControl)
				Style += (NumericFieldType == "String")?";text-align: left; padding-left: 2px; padding-right: 2px;":";text-align: right; padding-left: 2px; padding-right: 2px;";

			sb.Append((Password)?"<input type=\"password\" style=\"width:" + Width + m_DefaultStyle + Style + "\" ":"<input type=\"text\" style=\"width:" + Width + m_DefaultStyle + Style + "\" ");
			sb.Append(" ControlType=\"HInput\" ");
			sb.Append((DefaultValue != null)?renderDefaultValue():"");
			sb.Append((Required)?renderRequired():"");
			sb.Append((ReadOnly)?renderControlReadOnly():"");
			sb.Append((GridControl)?renderGridControl():"");
			sb.Append(" name=" + UniqueID + " value=\""+ Format(m_Value) +"\"");
			sb.Append((GridControl)?" InitialValue=\"\" ":" InitialValue=\""+m_Value+"\" ");
			sb.Append(" Formatted=\"true\" ");
			sb.Append((ToolTip)?" ToolTip=\"true\" title=\""+Format(Value)+"\" ":"");
			sb.Append((Positive)?" Positive=\"true\" ":"");
			sb.Append((StrictPositive)?" StrictPositive=\"true\" ":"");
			sb.Append((Negative)?" Negative=\"true\" ":"");
			sb.Append((StrictNegative)?" StrictNegative=\"true\" ":"");
			sb.Append(renderAlertInvalidString());
			sb.Append(renderAlertRequiredString());
			sb.Append(renderGridID());
			sb.Append((InvokeOnChange != "")?renderInvokeOnChange():"");
			sb.Append((InvokeOnActivate != "")?renderInvokeOnActivate():"");
			sb.Append((InvokeOnBeforeDeactivate != "")?renderInvokeOnBeforeDeactivate():"");
			sb.Append((InvokeOnChangeLine != "")?renderInvokeOnChangeLine():"");
			sb.Append((m_MaxLength > 0)?" MaxLength=\"" + m_MaxLength + "\" ":"");
			sb.Append(" Comma=\"" + CommaSep + "\" Decimal=\""+ DecimalSep + "\" Sufix=\"" + Sufix + "\" Prefix=\"" + Prefix + "\" nDecimals=\"" + DecimalsNo + "\" NumericFieldType=\"" + NumericFieldType + "\" ");
			sb.Append(" onactivate=\"OnHInputFocus(this);\" onbeforedeactivate=\"OnHInputBlur(this);\" ");
			sb.Append(" onkeydown=\"OnHControlKeyDown(this);\" onkeyup=\"OnHInputKeyUp(this);\" ondragenter=\"event.returnValue=false;\" />");

			output.Write(sb.ToString());
		}

		public void RaisePostDataChangedEvent()
		{
		}

	}
}
